package com.example.demo.controller;

import com.example.demo.annotations.NeedLogin;
import com.example.demo.dto.ResultDTO;
import com.example.demo.dto.ResultDTOBuilder;
import com.example.demo.service.ProcessService;
import com.example.demo.service.UserService;
import com.example.demo.utils.JWTUtil;
import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.ProcessEngines;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.Map;

@RestController
@CrossOrigin
public class ActivitiController {
    @Autowired
    private UserService userService;
    @Autowired
    private ProcessService processService;

    @GetMapping("/testException")
    public ResultDTO a(){
        return ResultDTOBuilder.success(2/0);
    }

    /**
     * 登录
     * username 用户名
     * password 密码
     */
    @PostMapping("/login")
    public ResultDTO login(@RequestBody Map<String,String> paramMap){
        return userService.login(paramMap.get("username"),paramMap.get("password"));

    }


    /**
     *初始化流程
     */
    @PostMapping("/init")
    @NeedLogin(role = "admin")
    public ResultDTO createProcess(){
        return ResultDTOBuilder.success(processService.init());
    }


    /**
     * 用户查看自己的实例
     * @param request
     * @return
     */
    @NeedLogin(role = "user")
    @PostMapping("/queryUserInstance")
    public ResultDTO queryAllTask(HttpServletRequest request){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.queryUserInstance(usernameFromToken));
    }

    /**
     * 用户递交任务
     */
    @NeedLogin(role = "user")
    @PostMapping("/userApplyTask")
    public ResultDTO userApplyTask(HttpServletRequest request,@RequestBody Map<String,String> map){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.userApplyTask(usernameFromToken,map));
    }

    /**
     * 根据实例编号查看该实例的历史流程
     * instanceId 实例编号
     */
    @NeedLogin(role="user")
    @PostMapping("/queryInstanceHistory")
    public ResultDTO queryInstanceHistory(@RequestBody Map<String,String> map){
        return ResultDTOBuilder.success(processService.queryInstanceHistory(map.get("instanceId")));
    }


    /**
     * 用户增加任务
     * @param request
     * cause 申请原因
     * day 申请时间
     * @return
     */
    @NeedLogin(role = "user")
    @PostMapping("/userAddTask")
    public ResultDTO userAddTask(HttpServletRequest request,@RequestBody Map<String,Object> map){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.userAddTask(usernameFromToken,map));
    }

    /**
     * 部门经理审批
     * isAgree yes/no 是否同意
     */
    @NeedLogin(role = "admin")
    @PostMapping("/adminTask")
    public ResultDTO adminTask(HttpServletRequest request,@RequestBody Map<String,String> map){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.adminTask(usernameFromToken,map.get("taskId"),map.get("isAgree")));
    }

    /**
     * 人事审批
     * isAgree yes/no 是否同意
     */
    @NeedLogin(role = "hr")
    @PostMapping("/hrTask")
    public ResultDTO hrTask(HttpServletRequest request,@RequestBody Map<String,String> map){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.adminTask(usernameFromToken,map.get("taskId"),map.get("isAgree")));
    }

    /**
     * 查询用户任务
     */
    @NeedLogin(role = "user")
    @PostMapping("/queryTask")
    public ResultDTO queryTask(HttpServletRequest request){
        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.queryTask(usernameFromToken));
    }

    /**
     * 根据任务Id查询任务数据
     * taskId 任务编号
     */
    @NeedLogin(role = "user")
    @PostMapping("/queryTaskData")
    public ResultDTO queryTaskData(HttpServletRequest request,@RequestBody Map<String,String> map){
//        String usernameFromToken = JWTUtil.getUsernameFromToken(request);
        return ResultDTOBuilder.success(processService.getTaskData(map.get("taskId")));
    }


}
